package cn.com.wangluotongxin.merchants.service.impl;

import cn.com.wangluotongxin.admin.DAdminGoodsCatalogService;
import cn.com.wangluotongxin.admin.DAdminQuickQueryService;
import cn.com.wangluotongxin.admin.model.AdminQuickQuery;
import cn.com.wangluotongxin.admin.vo.DGoodsCatalogVo;
import cn.com.wangluotongxin.common.constants.CommonConstant;
import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.pojo.TenantUser;
import cn.com.wangluotongxin.merchants.dto.GoodsInfoDto;
import cn.com.wangluotongxin.merchants.form.GoodsInfoEditForm;
import cn.com.wangluotongxin.merchants.form.GoodsInfoForm;
import cn.com.wangluotongxin.merchants.model.MerchantsGoodsInfo;
import cn.com.wangluotongxin.merchants.mapper.MerchantsGoodsInfoMapper;
import cn.com.wangluotongxin.merchants.model.constants.YesNo;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsInfoService;
import cn.com.wangluotongxin.merchants.vo.GoodsInfoVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 商户管理-商品信息 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-06-20
 */
@Service
public class MerchantsGoodsInfoServiceImpl extends ServiceImpl<MerchantsGoodsInfoMapper, MerchantsGoodsInfo> implements IMerchantsGoodsInfoService {

    @Reference
    private DAdminGoodsCatalogService goodsCatalogService;
    @Reference
    private DAdminQuickQueryService quickQueryService;

    @Override
    public IPage<GoodsInfoVo> getPage(GoodsInfoDto dto) {
        Page page = Page.of(dto.getPageNumber(), dto.getPageSize());
        return baseMapper.getPage(page, dto);
    }

    @Override
    public GoodsInfoVo detail(Integer id) {
        return baseMapper.detail(id);
    }

    @Override
    public MerchantsGoodsInfo add(GoodsInfoForm form, TenantUser tenantUser) {
        if(this.count(new LambdaQueryWrapper<MerchantsGoodsInfo>()
                .eq(MerchantsGoodsInfo::getGoodsName, form.getGoodsName())
                .ne(MerchantsGoodsInfo::getIsDeleted, CommonConstant.ONE)) > 0) {
            throw new BadResponseException("商品【"+ form.getGoodsName() +"】已存在");
        }
        MerchantsGoodsInfo goodsInfo = new MerchantsGoodsInfo();
        BeanUtils.copyProperties(form, goodsInfo);
        DGoodsCatalogVo goodsCatalogVo = goodsCatalogService.getById(form.getCatalogId());
        Assert.notNull(goodsCatalogVo, "分类不存在");

        if(form.getQuickQueryId() != null) {
            AdminQuickQuery quickQuery = quickQueryService.selectById(form.getQuickQueryId());
            Assert.notNull(quickQuery, "主目录不存在");
            goodsInfo.setQuickQueryId(quickQuery.getId());
            goodsInfo.setQuickQueryName(quickQuery.getQuickName());
        }

        goodsInfo.setCatalogId(goodsCatalogVo.getId());
        goodsInfo.setCatalogName(goodsCatalogVo.getCatalogName());
        goodsInfo.setCatalogPath(goodsCatalogVo.getPath());
        goodsInfo.setCreateUser(tenantUser.getId());
        goodsInfo.setCreateName(tenantUser.getUsername());
        goodsInfo.setModifyUser(tenantUser.getId());
        goodsInfo.setModifyName(tenantUser.getUsername());
        this.save(goodsInfo);

        String goodsNo = getGoodsNo(goodsInfo.getId());
        this.update(new LambdaUpdateWrapper<MerchantsGoodsInfo>()
                .set(MerchantsGoodsInfo::getGoodsNo, goodsNo)
                .eq(MerchantsGoodsInfo::getId, goodsInfo.getId()));
        return this.getById(goodsInfo.getId());
    }

    private String getGoodsNo(Integer id) {
        StringBuilder goodsNo = new StringBuilder("G");
        Integer strLength = 8 - id.toString().length();
        for(int i = 0; i < strLength; i ++) {
            goodsNo.append("0");
        }
        goodsNo.append(id.toString());
        return goodsNo.toString();
    }

    @Override
    public MerchantsGoodsInfo edit(GoodsInfoEditForm form, TenantUser tenantUser) {
        if(this.count(new LambdaQueryWrapper<MerchantsGoodsInfo>()
                .eq(MerchantsGoodsInfo::getGoodsName, form.getGoodsName())
                .ne(MerchantsGoodsInfo::getId, form.getId())
                .ne(MerchantsGoodsInfo::getIsDeleted, CommonConstant.ONE)) > 0) {
            throw new BadResponseException("商品【"+ form.getGoodsName() +"】已存在");
        }
        MerchantsGoodsInfo goodsInfo = new MerchantsGoodsInfo();
        BeanUtils.copyProperties(form, goodsInfo);
        if(form.getQuickQueryId() != null) {
            AdminQuickQuery quickQuery = quickQueryService.selectById(form.getQuickQueryId());
            Assert.notNull(quickQuery, "主目录不存在");
            goodsInfo.setQuickQueryName(quickQuery.getQuickName());
            goodsInfo.setQuickQueryId(quickQuery.getId());
        }
        goodsInfo.setModifyUser(tenantUser.getId());
        goodsInfo.setModifyName(tenantUser.getUsername());
        this.updateById(goodsInfo);
        return this.getById(goodsInfo.getId());
    }

    @Override
    public void delete(Integer id) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsInfo>()
                .set(MerchantsGoodsInfo::getIsDeleted, YesNo.YES.getCode())
                .eq(MerchantsGoodsInfo::getId, id));
    }
}
